Leaving Trails in Godot

How to make trails in Godot the easy way.

For my latest game protect@ I was thingking about adding trails to the falling squares. The idea behind that is to make the game kind of a bullet hell style where a lot is going on on the screen. Not yet sure if this game will be this way or not. But I already did my homework and figured out to draw some very basic trails:

  extends Line2D

  var target
  var point
  export (NodePath) var targetPath
  export var trailLength := -1          # -1: endless trail, 0: no trail, n: points in trail


  func _ready() -> void:
  	target = get_node(targetPath)


  func _process(delta:float) -> void:
    global_position = Vector2.ZERO
    global_rotation = 0
    point = target.global_position
    add_point(point)
    if trailLength != -1:
      while get_point_count() > trailLength:
        remove_point(0)

To be honest, this is not (all) my idea. 90% of the code is from a Youtube Video by KryperDev. The only thing I added was the option to make the trail endless.

How to use this piece: Make a new Line2D scene and add the script above. Link this scene in the one you want the trail to be drawn. In this scene (not the Line2D scene!) you have then the option to enter a NodePath as “Target Path”. Here you should select the object you wish to draw a trail. Choose a setting for the trail length (-1 for infinity, 0 to deactivate or any arbitrary length) and your object will leave a trail.

Admittedly, this is only a starting point as the script simply draws a line. No fancy, glowing particles. But I guess this could be changed by adding a material/ shader to the line. But except that they exist I know nothing about shaders. But that’s definitely something I should add to the list of things to look at.